/*
 *  Cero Project - Copyright   2006 The Cero Developement Team 
 *  (Michael Laguerre, Camille Roux, Matthieu Segret, Mathieu Sivade) 
 * 
 *  This program is free software; you can redistribute it and/or modify it 
 *  under the terms of the GNU General Public License as published by the Free 
 *  Software Foundation; either version 2 of the License, or (at your option) 
 *  any later version.
 * 
 *  This program is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 *  for more details.
 */

package org.ceroproject.ui.graphical.tools.base;

import java.awt.event.KeyEvent;

import org.ceroproject.ui.graphical.tools.Deck;
import org.ceroproject.ui.graphical.tools.GraphicsCard;
import org.ceroproject.ui.graphical.tools.GraphicsZone;
import org.ceroproject.ui.graphical.tools.UserInputListener;


/**
 * Class which select a Graphical Card
 * 
 * @author matt
 */
public class CardSelecter implements UserInputListener {

	/* -------------- field ------------- */

	/** older selected card */
	private GraphicsCard gCard;

	/* ------------- Method ------------- */

	/**
	 * The next card becomes the current card
	 */
	protected void nextCurrentCard(GraphicsZone gZone) {
		int i = 0;

		if (gZone != null) {
			if ((gCard != null) || (gZone.getGCardList().size() != 0)) {
				i = gZone.getGCardList().indexOf(gCard);
				if (i < gZone.getGCardList().size() - 1)
					i++;
				else
					i = gZone.getGCardList().size() - 1;

				gCard = gZone.getGCardList().get(i);
				gCard.setSelected(true);
			}
		}
	}

	/**
	 * The previous card becomes the current card
	 */
	protected void prevCurrentCard(GraphicsZone gZone) {
		int i = 0;
		if (gZone != null) {
			if ((gCard != null) || (gZone.getGCardList().size() != 0)) {
				i = gZone.getGCardList().indexOf(gCard);
				if (i > 0)
					i--;
				else
					i = 0;

				gCard = gZone.getGCardList().get(i);
				gCard.setSelected(true);
			}
		}
	}

	/**
	 * Select a graphical Card Invoked when a card has been clicked
	 * 
	 * @param selectedGCard
	 *            selected graphical Card
	 */
	public void onSelectedCardChanged(GraphicsCard selectedGCard) {

		// unselected the older selected card
		if (gCard != null)
			gCard.setSelected(false);

		// selected the new selected card
		if (selectedGCard != null)
			selectedGCard.setSelected(true);

		gCard = selectedGCard;
	}

	/**
	 * Select a graphical Zone Invoked when the zone has been clicked
	 * 
	 * @param selectedGZone
	 *            selected graphical Zone
	 */
	public void onZoneSelected(GraphicsZone selectedGZone) {
	}

	/**
	 * Invoked when a key has been pressed.
	 * 
	 * @param e
	 *            the KeyEvent to listen to
	 * @param deck
	 *            which is a graphical appearance of the game's deck
	 */
	public void onKeyPressed(KeyEvent e, Deck deck) {

		GraphicsZone gZone = null;

		if (gCard != null)
			gCard.setSelected(false);

		for (int i = 0; i < deck.getGraphicsZoneList().size(); i++) {
			if (deck.getGraphicsZoneList().get(i).getSelected())
				gZone = deck.getGraphicsZoneList().get(i);
		}

		switch (e.getKeyCode()) {

		case KeyEvent.VK_DOWN: {
			prevCurrentCard(gZone);
			break;
		}

		case KeyEvent.VK_UP: {
			nextCurrentCard(gZone);
			break;
		}

		case KeyEvent.VK_LEFT: {
			nextCurrentCard(gZone);
			break;
		}

		case KeyEvent.VK_RIGHT: {
			prevCurrentCard(gZone);
			break;
		}

		case KeyEvent.VK_ENTER: {
			// selected the new current card
			if (gCard != null)
				gCard.setSelected(true);
			break;
		}
		}

	}

	/**
	 * Return the current Card
	 * 
	 * @return the current Graphical Card
	 */
	public GraphicsCard getGCard() {
		return gCard;
	}

	/**
	 * Set the current Card
	 * 
	 * @param gc
	 *            the Graphical Card
	 */
	protected void setGCard(GraphicsCard gc) {
		gCard = gc;
	}

}
